package com.zyx.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.zyx.pojo.*;
import com.zyx.service.*;
import com.zyx.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@Controller
@RequestMapping("Views")
@ResponseBody
public class ViewsController {

    @Resource
    private AutoService autoService;

    @Resource
    private CollectService collectService;

    @Resource
    private UserService userService;

    @Autowired
    private NameService nameService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private RelevancyService relevancyService;

    @Autowired
    private SpencerService spencerService;

    @RequestMapping(value = "query", method = RequestMethod.POST)
    public List<Auto> queryName(String autoName) {
        List<Auto> list = new ArrayList<>();

        return list;
    }

    /**
     * 多条件查询车辆信息
     *
     * @param auto
     * @return java.util.List<com.zyx.pojo.Auto>
     * @author zyx
     * @date 8:32 2022/3/10
     * @name userAuto
     */
    @ResponseBody
    @RequestMapping(value = "userAuto", method = RequestMethod.GET)
    public List<Auto> userAuto(Auto auto) {
        List<Auto> list = new ArrayList<>();
        if (auto.getAutoName() != null && auto.getAutoName().equals("")) {
            auto.setAutoName(null);
        }
        if (auto.getRan() != null && auto.getRan().equals("")) {
            auto.setRan(null);
        }
        if (auto.getModel() != null && auto.getModel().equals("")) {
            auto.setModel(null);
        }
        if (auto.getEnergy() != null && auto.getEnergy().equals("")) {
            auto.setEnergy(null);
        }
        if (auto.getFirm() != null && auto.getFirm().equals("")) {
            auto.setFirm(null);
        }
        list = autoService.selectBy(auto);
        Map<String, Auto> map = new HashMap<>();
        List<Auto> l = new ArrayList<>();
        for (Auto a : list) {
            if (!map.containsKey(a.getAutoName())) {
                l.add(a);
                map.put(a.getAutoName(), a);
            }
        }
        return l;
    }

    /**
     * @Description: 根据前端传来的参数，查询汽车信息并返回
     * @Author: zyx
     * @Date: 19:30 2022/2/11
     * @Name: queryRan
     * @Param: [auto]
     * @Return: java.util.List<com.zyx.pojo.Auto>
     */
    @ResponseBody
    @RequestMapping(value = "queryAll", method = RequestMethod.POST)
    public List<Auto> queryRan(Auto auto) {
        List<Auto> list = autoService.queryAuto(auto);
        return list;
    }

    /**
     * @Description: 用户注册
     * @Author: zyx
     * @Date: 9:29 2022/3/8
     * @Name: userAdd
     * @Param: [user]
     * @Return: com.zyx.vo.ResultVo
     */
    @ResponseBody
    @RequestMapping(value = "userAdd", method = RequestMethod.POST)
    public ResultVo userAdd(User user) {
        user.setState(0);
        user.setCreateTime(new Date());
        int i = 0;
        try {
            i = userService.addUser(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (i == 1) {
            return new ResultVo<>();
        } else if (i == -1) {
            return new ResultVo<>(500, "手机号已经注册过，请换一个或者找回账号");
        }
        return new ResultVo<>(500, "添加失败！稍后再试！");
    }

    /**
     * @Description: 查找收藏榜前五的车辆信息
     * @Author: zyx
     * @Date: 16:12 2022/2/12
     * @Name: queryByTen
     * @Param: []
     * @Return: java.util.List<com.zyx.pojo.Auto>
     */
    @ResponseBody
    @RequestMapping(value = "queryByTen", method = RequestMethod.POST)
    public List<Name> queryByTen() {
        List<Name> list = new ArrayList<>();
        list = collectService.queryByTen();
        return list;
    }

    /**
     * @Description: 根据车辆id查询车辆信息
     * @Author: zyx
     * @Date: 16:59 2022/2/26
     * @Name: queryByAutoId
     * @Param: [autoId]
     * @Return: com.zyx.pojo.Auto
     */
    @ResponseBody
    @RequestMapping(value = "queryByAutoId/{id}", method = RequestMethod.GET)
    public Auto queryByAutoId(@PathVariable("id") Integer autoId) {
        return autoService.selectByKey(autoId);
    }

    //根据车型id查询相关车系车辆信息
    @ResponseBody
    @RequestMapping(value = "queryByModel", method = RequestMethod.POST)
    public List<Name> queryByModel(Integer id) {
        return nameService.queryByModel(id);
    }

    //根据车型id查询车辆名称和价格
    @ResponseBody
    @RequestMapping(value = "queryById", method = RequestMethod.GET)
    public List<Name> queryById(Integer id) {
        List<Name> list = nameService.queryById(id);
        for (Name name : list) {
            System.out.println(name);
        }
        return list;
    }

    //根据车型id和车辆id查询店铺
    @ResponseBody
    @RequestMapping(value = "querySpencer", method = RequestMethod.GET)
    public List<Spencer> querySpencer(int autoId, int carId) {
        System.out.println(autoId);
        System.out.println(carId);
        return relevancyService.querySpencer(autoId, carId);
    }

    //添加预约信息
    @ResponseBody
    @RequestMapping(value = "insertOrders", method = RequestMethod.POST)
    public ResultVo addOrders(Orders orders, String spencerName) {
        orders.setSpencerId(spencerService.queryById(spencerName));
        orders.setState(0);
        orders.setCreateTime(new Date());
        int i = orderService.addOrder(orders);
        if (i > 0) {
            return new ResultVo();
        } else {
            return new ResultVo(500, "预约失败，请重试");
        }
    }

    //根据条件查询车型信息
    @ResponseBody
    @RequestMapping(value = "queryByName", method = RequestMethod.GET)
    public List<Name> queryByName(Auto auto) {
        if (auto.getPrice2() != null && auto.getPrice() != null) {
            if (auto.getPrice() == 0 && auto.getPrice2() == 0) {
                auto.setPrice(null);
                auto.setPrice2(null);
            }else {
                if (auto.getPrice2() != 0 && auto.getPrice() == 0){
                    auto.setPrice(null);
                }
                if (auto.getPrice() != 0 && auto.getPrice2() == 0){
                    auto.setPrice2(null);
                }
            }
        }
        if (auto.getRan().equals("轿车")){
            auto.setRan("型车");
        }
        if (auto.getAutoName() != null && auto.getAutoName().equals("")) {
            auto.setAutoName(null);
        }
        if (auto.getRan() != null && auto.getRan().equals("")) {
            auto.setRan(null);
        }
        if (auto.getModel() != null && auto.getModel().equals("")) {
            auto.setModel(null);
        }
        if (auto.getEnergy() != null && auto.getEnergy().equals("")) {
            auto.setEnergy(null);
        }
        if (auto.getFirm() != null && auto.getFirm().equals("")) {
            auto.setFirm(null);
        }
        List<Name> list = nameService.queryByName(auto);
        for (Name n : list) {
            n.setPriceMax(autoService.queryMax(n.getCarName()));
            n.setPriceMin(autoService.queryMin(n.getCarName()));
        }
        return list;
    }

}
